# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=gammaray
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.1.0
pkgrel=3
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
pkgdesc="A tool for examining the internals of a Qt application and to some extent also manipulate it"
url="https://www.kdab.com/gammaray/"
msys2_repository_url="https://github.com/KDAB/GammaRay"
msys2_references=(
  'archlinux: gammaray'
)
license=('spdx:GPL-2.0-or-later')
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}-qt5")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-qt5")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-qt5")
depends=("${MINGW_PACKAGE_PREFIX}-qt6-base"
         "${MINGW_PACKAGE_PREFIX}-dlfcn")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-extra-cmake-modules"
             "${MINGW_PACKAGE_PREFIX}-qt6-3d"
             "${MINGW_PACKAGE_PREFIX}-qt6-connectivity"
             "${MINGW_PACKAGE_PREFIX}-qt6-declarative"
             "${MINGW_PACKAGE_PREFIX}-qt6-location"
             "${MINGW_PACKAGE_PREFIX}-qt6-positioning"
             "${MINGW_PACKAGE_PREFIX}-qt6-scxml"
             "${MINGW_PACKAGE_PREFIX}-qt6-shadertools"
             "${MINGW_PACKAGE_PREFIX}-qt6-svg"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools"
             "${MINGW_PACKAGE_PREFIX}-kcoreaddons-qt5"
             "${MINGW_PACKAGE_PREFIX}-kdstatemachineeditor-qt5"
             "${MINGW_PACKAGE_PREFIX}-qt5-3d"
             "${MINGW_PACKAGE_PREFIX}-qt5-connectivity"
             "${MINGW_PACKAGE_PREFIX}-qt5-declarative"
             "${MINGW_PACKAGE_PREFIX}-qt5-gamepad"
             "${MINGW_PACKAGE_PREFIX}-qt5-location"
             "${MINGW_PACKAGE_PREFIX}-qt5-script"
             "${MINGW_PACKAGE_PREFIX}-qt5-scxml"
             "${MINGW_PACKAGE_PREFIX}-qt5-svg"
             "${MINGW_PACKAGE_PREFIX}-qt5-tools"
             "${MINGW_PACKAGE_PREFIX}-syntax-highlighting-qt5"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-glslang")
optdepends=("${MINGW_PACKAGE_PREFIX}-qt6-3d: 3D plugins"
            "${MINGW_PACKAGE_PREFIX}-qt6-declarative: Quick/Qml and positioning plugins"
            "${MINGW_PACKAGE_PREFIX}-qt6-scxml: state machine viewer plugin"
            "${MINGW_PACKAGE_PREFIX}-qt6-connectivity: bluetooth plugin"
            "${MINGW_PACKAGE_PREFIX}-qt5-positioning: positioning plugin"
            "${MINGW_PACKAGE_PREFIX}-qt6-svg: Widget Export actions plugin"
            "${MINGW_PACKAGE_PREFIX}-qt6-tools: Widget Export actions plugin"
            "${MINGW_PACKAGE_PREFIX}-qt5-3d: Qt5 3D plugins"
            "${MINGW_PACKAGE_PREFIX}-qt5-base: Qt5 applications support"
            "${MINGW_PACKAGE_PREFIX}-qt5-declarative: Qt5 Quick/Qml and positioning plugins"
            "${MINGW_PACKAGE_PREFIX}-qt5-scxml: Qt5 state machine viewer plugin"
            "${MINGW_PACKAGE_PREFIX}-qt5-connectivity: Qt5 bluetooth plugin"
            "${MINGW_PACKAGE_PREFIX}-qt5-script: Qt5 script engine debugger plugin"
            "${MINGW_PACKAGE_PREFIX}-qt5-location: Qt5 positioning plugin"
            "${MINGW_PACKAGE_PREFIX}-qt5-svg: Qt5 Widget Export actions plugin"
            "${MINGW_PACKAGE_PREFIX}-qt5-tools: Qt5 Widget Export actions plugin"
            "${MINGW_PACKAGE_PREFIX}-kcoreaddons-qt5: KJob tracker plugin"
            "${MINGW_PACKAGE_PREFIX}-kdstatemachineeditor-qt5: ")
source=(${msys2_repository_url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz
        003-win32-not-elf.patch
        004-files-layout.patch
        005-fix-dll-naming.patch
        006-qt-6.9.patch::https://github.com/KDAB/GammaRay/pull/1028.patch)
sha256sums=('6b8e3005105bd9f7553a64fab68e724840b5cb411f3115f5a8577f999b2e9254'
            'b13e2caa213d0b83a37c72e57bb5dc6e1d7e3d06d3f3031ad8931df3059cd9c0'
            '5c7e44e3fa6e914e369c2149fe9ca4b9e133f6753c7c6539cef66c9aeac5da21'
            '1d4ebacca418f8f1c9df0c4c438742e63d5d372d37d1605ecf9b478b098155eb'
            '047d6b7ba7dc50b1161a1d6d945b7e741e3677b22499a8b98761fad8c87c53c4')
validpgpkeys=(E86C000370B1B9E2A9191AD53DBFB6882C9358FB) # KDAB Products <info@kdab.com>

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd ${_realname}-${pkgver}
  apply_patch_with_msg \
    003-win32-not-elf.patch \
    004-files-layout.patch \
    005-fix-dll-naming.patch \
    006-qt-6.9.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    _extra_config+=("-DSTACK_DETAILS_BFD=ON")
  else
    _extra_config+=("-DSTACK_DETAILS_BFD=OFF")
  fi

  for _qt_ver in 5 6; do
    if [[ ${_qt_ver} == 5 ]]; then
      _include_dir=include
    else
      _include_dir=include/qt6
    fi
    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DECM_MKSPECS_INSTALL_DIR=;-DPLUGIN_INSTALL_DIR=;" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -G "Ninja" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_INSTALL_OLDINCLUDEDIR=${MINGW_PREFIX}/${_include_dir} \
      -DTRANSLATION_INSTALL_DIR=${MINGW_PREFIX}/share/qt${_qt_ver}/translations \
      -DPLUGIN_INSTALL_DIR=${MINGW_PREFIX}/share/qt${_qt_ver}/plugins/gammaray \
      -DECM_MKSPECS_INSTALL_DIR=${MINGW_PREFIX}/share/qt${_qt_ver}/mkspecs/modules \
      -DGAMMARAY_INSTALL_QT_LAYOUT=ON \
      -DGAMMARAY_MULTI_BUILD=FALSE \
      -DGAMMARAY_WITH_KDSME=OFF \
      -DSTACK_DETAILS_AUTO_DETECT=OFF \
      -DMINGW_MSVCR_LIBRARY="" \
      -DQT_VERSION_MAJOR=${_qt_ver} \
      "${_extra_config[@]}" \
      -S ${_realname}-${pkgver} \
      -B build-${MSYSTEM}-qt${_qt_ver}

    ${MINGW_PREFIX}/bin/cmake.exe --build build-${MSYSTEM}-qt${_qt_ver}
  done
}

package() {
  for _qt_ver in 5 6; do
    DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install build-${MSYSTEM}-qt${_qt_ver}
  done

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
